package com.epoint.mobileoa.ui.wsgw;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.NewTask;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.tools.StringHelp;
import com.epoint.mobileoa.tools.ToastHelp;
import com.epoint.mobileoa.tools.XMLUtil;
import com.epoint.mobileoa.ui.wsgw.model.Handle_GetUnHandleList_Model;

/**
 * @author liyc
 * @time 2012-12-14 上午10:18:03
 * @annotation 调用Webservice获取公文流转列表任务
 */
public class WsDbListView extends SuperView {

	private ListView lv;
	private LinearLayout foot;
	private int CurrentPageIndex = 1;//第几页
	private int PageSize = 20;//每页显示数量
	private List<Handle_GetUnHandleList_Model> list = new ArrayList<Handle_GetUnHandleList_Model>();
	private ListAdapter adapter = new ListAdapter();
	@Override
	public void initControls() {
		lv = (ListView) findViewById(R.id.lv);
		lv.setOnItemClickListener(this);
		addFoot();
		getData();
		lv.setAdapter(adapter);
	}

	private void getData(){
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("CurrentPageIndex", CurrentPageIndex);
		param.put("PageSize", PageSize);
		new GetDbListTask(this,1,param, null, NewTask.ProgressTop);
		CurrentPageIndex ++;
	}

	public void addFoot(){
		TextView load_bt = new TextView(this);
		load_bt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
		load_bt.setText("更多...");
		load_bt.setGravity(Gravity.CENTER);
		foot = new LinearLayout(this);
		foot.addView(load_bt, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		foot.setGravity(Gravity.CENTER);
		lv.addFooterView(foot);
		registerForContextMenu(lv);
	}

	@Override
	public void refresh(Object... param) throws Exception {
	}

	@Override
	public int getLayoutInt() {
		return R.layout.ws_list;
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
		String bs = param[0].toString();
		String ReturnInfo = StringHelp.getXMLAtt(bs, "ReturnInfo");
		String Status = StringHelp.getXMLAtt(ReturnInfo, "Status");
		if(Status.equals("True"))
		{
			int TotalNumCount =  Integer.parseInt(StringHelp.getXMLAtt(bs, "TotalNumCount"));
			String listxml = StringHelp.getXMLAttOut(bs, "HandleList");
			List<Handle_GetUnHandleList_Model> listtmp = XMLUtil.DomAnalysisCommon(listxml, Handle_GetUnHandleList_Model.class);
			for(Handle_GetUnHandleList_Model a:listtmp)
			{
				list.add(a);
				adapter.notifyDataSetChanged();
			}
			if(listtmp.size()<20)
			{
				ToastHelp.showToast(this, "加载完毕");
				lv.removeFooterView(foot);
			}
			System.out.println("TotalNumCount:"+TotalNumCount);
			System.out.println("list.size():"+list.size());
		}
		else{
			ToastHelp.showAndroidToastWorning(this, StringHelp.getXMLAtt(bs, "Description"));
		}

	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		if(position==list.size()){
			getData();
			return;
		}
		Intent intent = new Intent(this, WsDbDetailView.class);
		Handle_GetUnHandleList_Model h = list.get(position);
		String type = "待办详情";
		if(h.FileType.equals("SW"))
		{
			type = "收文详情";
		}
		else if(h.FileType.equals("FW"))
		{
			type = "发文详情";
		}
		intent.putExtra("viewtitle",type);
		intent.putExtra("model", h);
		System.out.println(h.MessageItemGuid);
		startActivity(intent);
	}

	class ListAdapter extends BaseAdapter
	{

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				LayoutInflater mInflater = LayoutInflater.from(getContext());
				convertView = mInflater.inflate(R.layout.achive_lv_style, null);
				holder.achive_lv_type = (TextView) convertView
						.findViewById(R.id.achive_lv_type);
				holder.achive_lv_title = (TextView) convertView
						.findViewById(R.id.achive_lv_title);
				holder.achive_lv_blr = (TextView) convertView
						.findViewById(R.id.achive_lv_blr);
				holder.achive_lv_tjr = (TextView) convertView
						.findViewById(R.id.achive_lv_tjr);
				holder.achive_lv_bzmc = (TextView) convertView
						.findViewById(R.id.achive_lv_bzmc);
				holder.achive_lv_fssj = (TextView) convertView
						.findViewById(R.id.achive_lv_fssj);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			Handle_GetUnHandleList_Model h = list.get(position);
			
			holder.achive_lv_title.setText(h.Title);
			
			String type = "待办";
			if(h.FileType.equals("SW"))
			{
				type = "收文";
			}
			else if(h.FileType.equals("FW"))
			{
				type = "发文";
			}
			holder.achive_lv_type.setText(type);
			holder.achive_lv_tjr.setText("提交人："+h.FromDispName);
			holder.achive_lv_blr.setVisibility(View.GONE);
			holder.achive_lv_bzmc.setVisibility(View.GONE);
			holder.achive_lv_fssj.setText("发送日期："+h.GenerateDate);

			return convertView;
		}
	}
	
	public class ViewHolder {
		public TextView achive_lv_type;
		public TextView achive_lv_title;
		public TextView achive_lv_blr;
		public TextView achive_lv_tjr;
		public TextView achive_lv_bzmc;
		public TextView achive_lv_fssj;
	}

	@Override
	public String setTitle() {
		return "待办公文";
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
	}
}
